package com.yfbao.horizon.inspection.engine.impl;

import com.yfbao.horizon.inspection.config.InspectConfig;
import com.yfbao.horizon.inspection.config.ReportTaskConfig;
import com.yfbao.horizon.inspection.dto.ReportDto;
import com.yfbao.horizon.inspection.dto.ReportInfoResult;
import com.yfbao.horizon.inspection.dto.ReportResult;
import com.yfbao.horizon.inspection.entity.SystemInspectRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * 巡检报告生成引擎实现类
 * 负责根据巡检指标采集结果集生成巡检报告
 */
@Component
public class ReportGeneratorEngine extends AbstractInspectEngine<ReportInfoResult> {

    private static final Logger logger = LoggerFactory.getLogger(ReportGeneratorEngine.class);

    /**
     * 引擎编码
     */
    private static final String ENGINE_CODE = "report_generator";

    @Override
    public String getCode() {
        return ENGINE_CODE;
    }

    @Override
    public List<ReportInfoResult> getReportInfo(String taskNo) {
        // 实际应用中，这里应该从数据库查询对应的报告详情
        logger.info("获取任务 {} 的报告详情", taskNo);
        return new ArrayList<>();
    }

    @Override
    public ReportDto inspectCustomerReportResult(InspectConfig config, ReportTaskConfig reportConfig) {
        logger.info("执行自定义巡检报告生成，任务编号: {}, 环境: {}", config.getTaskNo(), config.getInspectEnv());
        
        // 自定义报告生成逻辑
        String taskNo = config.getTaskNo() != null ? config.getTaskNo() : generateTaskNo();
        config.setTaskNo(taskNo);
        
        List<ReportInfoResult> infoList = new ArrayList<>();
        List<String> levels = new ArrayList<>();
        
        // 这里应该根据自定义逻辑获取报告详情
        // 示例：创建一个示例报告详情
        ReportInfoResult info = new ReportInfoResult();
        info.setTaskNo(taskNo);
        info.setEnv(config.getInspectEnv());
        info.setInspectCategory(config.getInspectCategory());
        info.setInspectItem(config.getInspectItem());
        info.setCaption("自定义检查");
        info.setLabel("自定义检查结果");
        info.setLevel("none");
        info.setMessage("正常");
        info.setSort(1);
        info.setCollectTime(getCurrentTimeString());
        
        infoList.add(info);
        levels.add("normal");
        
        String maxLevel = getMaxLevel(levels);
        ReportResult reportResult = createReportResult(config, reportConfig, maxLevel);
        reportResult.setInspectDesc("自定义巡检完成");
        
        return createReportDto(reportResult, infoList);
    }

    @Override
    public ReportDto inspectReportResult(InspectConfig config, ReportTaskConfig reportConfig) {
        logger.info("执行规则巡检报告生成，任务编号: {}, 环境: {}", config.getTaskNo(), config.getInspectEnv());
        
        String taskNo = config.getTaskNo() != null ? config.getTaskNo() : generateTaskNo();
        config.setTaskNo(taskNo);
        
        List<ReportInfoResult> infoList = new ArrayList<>();
        List<String> levels = new ArrayList<>();
        
        // 计算统计周期的开始时间
        LocalDateTime startTime = LocalDateTime.now().minusDays(reportConfig.getStatCycle());
        logger.info("统计周期: {} 天，开始时间: {}", reportConfig.getStatCycle(), startTime);
        
        // 实际应用中，这里应该从数据库查询统计周期内的巡检记录
        // 根据统计规则进行计算和汇总
        
        // 示例：创建一个示例报告详情
        ReportInfoResult info = new ReportInfoResult();
        info.setTaskNo(taskNo);
        info.setEnv(config.getInspectEnv());
        info.setInspectCategory(config.getInspectCategory());
        info.setInspectItem(config.getInspectItem());
        info.setCaption("规则检查");
        info.setLabel("规则检查结果");
        info.setLevel("none");
        info.setMessage("正常");
        info.setSort(1);
        info.setCollectTime(getCurrentTimeString());
        
        infoList.add(info);
        levels.add("normal");
        
        String maxLevel = getMaxLevel(levels);
        ReportResult reportResult = createReportResult(config, reportConfig, maxLevel);
        reportResult.setInspectDesc("规则巡检完成，统计周期: " + reportConfig.getStatCycle() + " 天");
        
        return createReportDto(reportResult, infoList);
    }

    @Override
    public SystemInspectRecord inspectCustomerCollect(InspectConfig config) {
        logger.info("执行自定义巡检指标采集，项目: {}, 环境: {}", config.getInspectItem(), config.getInspectEnv());
        return doInspect(config);
    }

    @Override
    public SystemInspectRecord inspectCollect(InspectConfig config) {
        logger.info("执行规则巡检指标采集，项目: {}, 环境: {}", config.getInspectItem(), config.getInspectEnv());
        return doInspect(config);
    }

    @Override
    public void inspectPeriodScan(InspectConfig config) {
        logger.info("执行周期扫描，项目: {}, 环境: {}", config.getInspectItem(), config.getInspectEnv());
        // 执行周期扫描逻辑，将结果保存到数据库
        SystemInspectRecord record = doInspect(config);
        // 实际应用中，这里应该保存record到数据库
        logger.info("周期扫描完成，结果: {}", record.getInspectItemResult());
    }

    @Override
    protected SystemInspectRecord doInspect(InspectConfig config) {
        SystemInspectRecord record = new SystemInspectRecord();
        record.setInspectEnv(config.getInspectEnv());
        record.setInspectCategory(config.getInspectCategory());
        record.setInspectItem(config.getInspectItem());
        record.setInspectItemResult("normal");
        record.setInspectItemValue("0");
        record.setInspectItemDesc("检查通过");
        record.setCollectTime(LocalDateTime.now());
        return record;
    }
}